package pda.control;

import java.awt.*;
import java.awt.event.*;
import javax.swing.JPanel;
import pda.datas.CheckersData;
import pda.view.*;


public class CheckersCtrl implements IApplication, ActionListener, MouseListener, MouseWheelListener, MouseMotionListener
{
    private String name = "Checkers";
    private CheckersData data;
    private CheckersView view;
    
    public CheckersCtrl()
    {
        data = new CheckersData(10);
        view = new CheckersView(this, data);
        data.setView(view);
        data.updateLastMove();
    }
    
    @Override
    public void start(PdaCtrl pda)
    {
        view.createBuffers();
        new Thread(data).start();
    }

    @Override
    public String getAppliName()
    {
        return name;
    }

    @Override
    public JPanel getAppliPanel()
    {
        return view.getPanel();
    }

    @Override
    public boolean close()
    {
        data.stopAI();
        data.quit = true;
        data.startAI();
        return true;
    }

    @Override
    public void setAppliName(String theName)
    {
    }

    @Override
    public void mouseClicked(MouseEvent me)
    {
    }

    @Override
    public void mousePressed(MouseEvent me)
    {
        if(me.getSource() == view.turn)
        {
            if(data.positionEdit) data.toggleTurn();
            return;
        }
        
        Point p = me.getPoint();
        if(data.positionEdit)
        {
            if(me.getButton() == MouseEvent.BUTTON1)
            {
                int square = view.coordToSquare(p.x, p.y);
                if(square == -1) return;
                data.check.setSquare(square, view.dragPiece);
                view.updateScreen();
            }
            else if(me.getButton() == MouseEvent.BUTTON3)
            {
                int square = view.coordToSquare(p.x, p.y);
                if(square == -1) return;
                data.check.setSquare(view.coordToSquare(p.x, p.y), -1);
                view.updateScreen();
            }
        }
        else
        {
            if(me.getButton() == MouseEvent.BUTTON1)
                data.setDrag(p);
            else if(me.getButton() == MouseEvent.BUTTON3 && view.dragSquare<0)
                data.previousMove();
        }     
    }

    @Override
    public void mouseReleased(MouseEvent me)
    {
        if(me.getSource() == view.turn || !view.isDragging() || data.positionEdit)
            return;
        
        Point p = me.getPoint();
        data.play(p.x, p.y);
        view.updateScreen();
    }

    @Override
    public void mouseEntered(MouseEvent me)
    {
    }

    @Override
    public void mouseExited(MouseEvent me)
    {
    }

    @Override
    public void mouseDragged(MouseEvent me)
    {
        Point p = me.getPoint();
        view.updateDragCoord(p);
        if(!data.positionEdit) view.destSquare = view.coordToSquare(p.x, p.y);
    }

    @Override
    public void mouseMoved(MouseEvent me)
    {
        if(!data.positionEdit) return;
        Point p = me.getPoint();
        view.updateDragCoord(p);
    }

    @Override
    public void mouseWheelMoved(MouseWheelEvent mwe)
    {
        if(data.positionEdit)
        {
            if(mwe.getWheelRotation()>0)
                view.decrementDragPiece();
            else
                view.incrementDragPiece();
        }
        else
        {
            if(view.dragSquare>0) return;
            if(mwe.getWheelRotation()>0)
                data.previousMove();
            else
                data.nextMove();
        }
    }
    
    @Override
    public void actionPerformed(ActionEvent ae)
    {   
        if(ae.getSource() == view.newGame)
            data.newGame();
        else if(ae.getSource() == view.loadGame)
            data.loadGame();
        else if(ae.getSource() == view.saveGame)
            data.saveGame();
        else if(ae.getSource() == view.showNumbersCheck)
            view.toggleNumbers();
        else if(ae.getSource() == view.showLastMovCheck)
            view.toggleLastMove();
        else if(ae.getSource() == view.showValidCheck)
        {view.showValidMoves ^= true; view.updateScreen();}
        else if(ae.getSource() == view.flipBoardCheck)
        {view.flipBoard ^= true; view.updateScreen();}
        else if(ae.getSource() == view.editPosCheck)
            data.togglePositionEdit();
        else if(ae.getSource() == view.emptyBoard)
            data.emptyBoard();
        else if(ae.getSource() == view.ai1Check)
            data.toggleWhite();
        else if(ae.getSource() == view.ai2Check)
            data.toggleBlack();
        else if(ae.getSource() == view.moveNow)
            data.moveNow();
        else
            data.setDepth(Integer.valueOf(ae.getActionCommand()));
    }
}
